<?=$ind?><h2>Inscription</h2>
<?=$ind?><p class="descr">
<?=$ind?>	Le <strong>CMI GAME<sup>&reg;</sup></strong> est un jeu r&eacute;serv&eacute; aux joueurs poss&eacute;dant un compte de jeu.<br />
<?=$ind?>	L'inscription au service reste cependant gratuite, et ne n&eacute;cessite qu'une adresse e-mail valide.<br />
<?=$ind?>	<br />
<?=$ind?>	Pour commencer votre inscription, veuillez remplir le formulaire ci-dessous.<br />
<?=$ind?></p>

<?php

	$errors = array("notyet");
	$error_fields = array();

	$genders = array(
		'0'=>"non renseigné",
		'm'=>"masculin",
		'f'=>"féminin"
	);
	$gender_keys = array_keys($genders);
	foreach ($gender_keys as $gk) {
		$genders[$gk] = utf8_decode($genders[$gk]);
	}

	$account = array(
		'login'		=> array('min'=>5, 'max'=>15),
		'passwd'	=> array('min'=>7, 'max'=>20),
	);

	$login = "";
	$lastname = "";
	$firstname = "";
	$email = "";
	$gender = "0";

	$errors = array();
	$show_form = 1;

	if (isset($_POST['login'])) {


		$login = $_POST['login'];
		$lastname = $_POST['lastname'];
		$firstname = $_POST['firstname'];
		$email = $_POST['email'];
		$passwd = $_POST['passwd'];
		$passwd2 = $_POST['passwd2'];
		$gender = $_POST['gender'];

		/* Traitement */
			/* Login */
		//if ( !eregi("^([a-zA-Z0-9_]{".$account['login']['min'].",".$account['login']['max']."})$",$login) ) echo "login non valide";
		if (strlen($login)<$account['login']['min']) {
			array_push($errors,array("login","Votre identifiant est trop court"));
			array_push($error_fields,"login");
		}
		if (strlen($login)>$account['login']['max']) {
			array_push($errors,array("login","Votre identifiant est trop long"));
			array_push($error_fields,"login");
		}
		if ( !eregi("^([a-zA-Z0-9_]*)$",$login) ) {
			array_push($errors,array("login","Votre identifiant contient des caract&egrave;res interdits"));
			array_push($error_fields,"login");
		}
			/* Nom */
		if ( !eregi("^([a-zA-Z0-9 -]*)$",$lastname) ) {
			array_push($errors,array("lastname","Votre nom contient des caract&egrave;res interdits"));
			array_push($error_fields,"lastname");
		}
			/* Prénom */
		if ( !eregi("^([a-zA-Z0-9 -]*)$",$firstname) ) {
			array_push($errors,array("firstname","Votre pr&eacute;nom contient des caract&egrave;res interdits"));
			array_push($error_fields,"firstname");
		}
			/* Email */
		if ( !eregi("^"."([a-zA-Z]{1})"."([a-zA-Z0-9_.-]*)"."@"."([a-zA-Z]{1})"."([a-zA-Z0-9_.-]*)"."\\."."([a-zA-Z]{2,4})"."$",$email) ) {
			array_push($errors,array("email","Veuillez entrer une adresse email valide"));
			array_push($error_fields,"email");
		}
		$email = strtolower($email);
			/* Passwords */
		if ( strlen($passwd)<$account['passwd']['min']) {
			array_push($errors,array("passwd","Votre mot de passe est trop court"));
			array_push($error_fields,"passwd");
		}
		if ( strlen($passwd)>$account['passwd']['max']) {
			array_push($errors,array("passwd","Votre mot de passe est trop long"));
			array_push($error_fields,"passwd");
		}
		if ($passwd!=$passwd2) {
			array_push($errors,array("passwd2","Les deux mots de passe ne correspondent pas"));
			array_push($error_fields,"passwd2");
		}
			/* Gender */
		if (!array_key_exists($gender,$genders)) {
			array_push($errors,array("gender","Veuillez choisir un sexe valide"));
			array_push($error_fields,"gender");
		}

		//print_r($errors);

		if (!count($errors)) {
			$passwd = addslashes($passwd);
			//TODO : $passwd = crypt($passwd);
			/* Date du jour */
			$date = strftime("%Y-%e-%d %H:%M:%S");
			$conf_code = random_code(8);
			$query = "INSERT INTO `".$db_table['member']."` (`login`,`email`,`lastname`,`firstname`,`passwd`,`gender`,`date_reg`,`conf_code`)"
			." VALUES ('{$login}','{$email}','{$lastname}','{$firstname}','{$passwd}','".substr($gender,0,1)."','{$date}','".$conf_code."');";
			//echo "<i>$query</i><br />\n";
			$query = mysql_query($query);
			$errno = mysql_errno();
			if ($errno) {
				switch ($errno) {
					//FIXME : compléter les cas
					case '1062':
						//FIXME : compléter les cas de champs
						if (eregi("^Duplicate entry '"."(.*)"."' for key "."([0-9]{1,})"."$",mysql_error(),$e)) {
							$v = $e[1];
							$k = $e[2]*1;
							switch ($k) {
								case 2:	//login
									array_push($errors,array("login","Le pseudonyme &quot;$login&quot; est d&eacute;j&agrave; utilis&eacute;. (<a href=\"?id=rooms\">Jouer</a> / <a href=\"?activate=$login\">Activer mon compte</a>)"));
									array_push($error_fields,"login");
								break;
								case 3:	//email
									array_push($errors,array("email","Un compte est d&eacute;j&agrave; associ&eacute; &agrave; l'addresse email &quot;$email&quot;. (<a href=\"?id=rooms\">Jouer</a> / <a href=\"?id=activate\">Activer mon compte</a>)"));
									array_push($error_fields,"email");
								break;
								default : echo $errno;
							}
						} else {
							//TODO : sauver l'erreur inattendue
							array_push($errors("","Une erreur impr&eacute;vue s'est produite. Votre compte n'a pas pu être cr&eacute;&eacute; (".mysql_error().")"));
						}
					break;
					default:
						//TODO : sauver l'erreur inattendue
						array_push($errors("","Une erreur impr&eacute;vue s'est produite. Votre compte n'a pas pu être cr&eacute;&eacute; (SQL n&deg;$errno) &raquo; ".mysql_error().""));
				}
			}

			// Si toujours pas d'erreur
			if (!count($errors)) {
				/* Envoi du code de confirmation */
				$exp="no-reply@fssimock.fr";
				//FIXME : changer le titre
				$titre = "Activez votre compte ".$app['name'];
				$message = "";
				$message .= "<html>\n";
				$message .= "<head>\n";
				$message .= "</head>\n";
				$message .= "<body>\n";
				$message .= "<h3>Inscription sur ".$app['name']."</h3>\n";
				$message .= "Afin de compl&eacute;ter votre inscription, veuillez cliquer sur le lien suivant :<br />\n";
				$link = $app['address']."?activate=$login&amp;code=$conf_code";
				$message .= "<a href=\"$link\">$link</a><br />\n";
				$message .= "<br />\n";
				$message .= "</body>\n";
				$message .= "</html>\n";
				$headers  = 'MIME-Version: 1.0' . "\n";
				$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
			// 	En-têtes additionnels
				$headers .= "To: $email\n";
				$headers .= "From: $exp\n";
				mail($email, $titre, $message, $headers);
				$show_form = 0;
?>
<?=$ind?><div class="created">
<?=$ind?>	<p>Votre compte a bien &eacute;t&eacute; cr&eacute;&eacute;.</p>
<?=$ind?>	<p>Afin d'activer votre inscription, veuillez vous rendre dans votre bo&icirc;te aux lettres et suivre le lien de confirmation.</p>
<?=$ind?>	<p>Ou en copiant votre code dans la <a href="?activate=<?=$login?>" title="Activer mon compte">page suivante</a>.</p>
<?=$ind?></div>
<?php
			}
		}

		// Affichage des erreurs
		if ( count($errors) ) {
?>
<?=$ind?><div class="errors">
<?=$ind?>	<h3>Erreurs</h3>
<?=$ind?>	<p>Votre inscription n'a pas pu &ecirc;tre termin&eacute;e, veuillez corriger les erreurs suivantes :</p>
<?=$ind?>	<ul>
<?php
				foreach ($errors as $e) {
?>
<?=$ind?>		<li><?=$e[1]?></li>
<?php
				}
?>
<?=$ind?>	</ul>
<?=$ind?></div>
<?php
		}

	} //POSTS


	//print_r($errors);


	if ($show_form) {

		function errshow ($field) {
			global $error_fields;
			if (in_array($field,$error_fields)) return " class=\"error\"";
			return "";
		}

		$login = stripslashes($login);
		$lastname = stripslashes($lastname);
		$firstname = stripslashes($firstname);
		$email = stripslashes($email);


?>
<?=$ind?><form action="" method="post">
<?=$ind?>	<table id="register">
<?=$ind?>		<tr<?=errshow("login")?>>
<?=$ind?>			<td class="field">Pseudonyme :</td>
<?=$ind?>			<td><input type="text" name="login" value="<?=htmlentities($login)?>" /></td>
<?=$ind?>			<td class="guide"></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("lastname")?>>
<?=$ind?>			<td class="field">Nom :</td>
<?=$ind?>			<td class="guide"><input type="text" name="lastname" value="<?=htmlentities($lastname)?>" /></td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("firstname")?>>
<?=$ind?>			<td class="field">Pr&eacute;nom :</td>
<?=$ind?>			<td class="guide"><input type="text" name="firstname" value="<?=htmlentities($firstname)?>" /></td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("email")?>>
<?=$ind?>			<td class="field">E-mail :</td>
<?=$ind?>			<td class="guide"><input type="text" name="email" value="<?=htmlentities($email)?>" /></td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("passwd")?>>
<?=$ind?>			<td class="field">Mot de passe :</td>
<?=$ind?>			<td class="guide"><input type="password" name="passwd" /></td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("passwd2")?>>
<?=$ind?>			<td class="field">Confirmez :</td>
<?=$ind?>			<td class="guide"><input type="password" name="passwd2" /></td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr<?=errshow("gender")?>>
<?=$ind?>			<td class="field">Sexe :</td>
<?=$ind?>			<td class="guide">
<?=$ind?>				<select name="gender">
<?php
		$gender_keys = array_keys($genders);
		foreach ($gender_keys as $gk) {
?>
<?=$ind?>					<option value="<?=$gk?>"<?
							if ($gk==$gender) echo " selected=\"selected\"";
						?>><?=htmlentities($genders[$gk])?></option>
<?php
		}
?>
<?=$ind?>				</select>
<?=$ind?>			</td>
<?=$ind?>			<td></td>
<?=$ind?>		</tr>
<?=$ind?>		<tr class="buttons">
<?=$ind?>			<td colspan="3">
<?=$ind?>				<input type="submit" value="M'inscrire" />
<?=$ind?>				<input class="cancel" type="button" value="Annuler" onclick="document.location.href='.';" />
<?=$ind?>			</td>
<?=$ind?>		</tr>
<?=$ind?>	</table>
<?=$ind?></form>
<?php
		}
?>

